home *** CD-ROM | disk | FTP | other *** search
- #include <math.h>
- #include <stdio.h>
- #include <assert.h>
-
- void xmath();
-
- int roughly(double x, double y)
- {
- if(y==0.0)
- return (fabs(x) < 1.0e-6);
- else
- return (fabs((y-x)/y) < 1.0e-6);
- }
-
- int main()
- {
- xmath();
- }
-
- void xmath()
- {
- double roottwo= 1.4142135623730950488016887242097; // --- from Windows calculator
- double my_pi= 3.1415926535897932384626433832795;
- double my_e= 2.71828182845904523536028747135266;
-
- printf("==> starting xmath <==\n");
-
-
- // --- test the ZMATH functions
- // ----------------------------
-
- // --- test sqrt
- assert(roughly(sqrt(0.0),0.0));
- assert(roughly(sqrt(1.0),1.0));
- assert(roughly(sqrt(-1.0),0.0));
-
- assert(roughly(sqrt(25.0),5.0));
- assert(roughly(sqrt(-25.0),0.0));
-
- assert(roughly(sqrt(2.0),roottwo));
- assert(roughly(sqrt(0.5),1.0/roottwo));
- printf("passed sqrt... ");
-
- // --- test sqrt
- assert(ceil(5.5)==6.0); assert(ceil(-5.5)==-5.0);
- assert(ceil(6.6)==7.0); assert(ceil(-6.6)==-6.0);
- assert(ceil(700.7)==701.0); assert(ceil(-700.7)==-700.0);
-
- assert(floor(5.5)==5.0); assert(floor(-5.5)==-6.0);
- assert(floor(6.6)==6.0); assert(floor(-6.6)==-7.0);
- assert(floor(700.7)==700.0); assert(floor(-700.7)==-701.0);
-
- assert(ceil(2.0)==2.0); assert(ceil(-2.0)==-2.0);
- assert(floor(2.0)==2.0); assert(floor(-2.0)==-2.0);
- printf("passed ceil/floor... ");
-
- // --- test sin, cos, tan
- assert(roughly(sin(0.0),0.0)); // --- test sine -pi to +pi
- assert(roughly(sin(0.25 * my_pi),1.0/roottwo));
- assert(roughly(sin(0.5 * my_pi),1.0));
- assert(roughly(sin(0.75 * my_pi),1.0/roottwo));
- assert(roughly(sin(1.0 * my_pi),0.0));
-
- assert(roughly(sin(-0.25 * my_pi),-1.0/roottwo));
- assert(roughly(sin(-0.5 * my_pi),-1.0));
- assert(roughly(sin(-0.75 * my_pi),-1.0/roottwo));
- assert(roughly(sin(-1.0 * my_pi),0.0));
-
- assert(roughly(cos(0.0),1.0)); // --- test cosine -pi to +pi
- assert(roughly(cos(0.25 * my_pi),1.0/roottwo));
- assert(roughly(cos(0.5 * my_pi),0.0));
- assert(roughly(cos(0.75 * my_pi),-1.0/roottwo));
- assert(roughly(cos(1.0 * my_pi),-1.0));
-
- assert(roughly(cos(-0.25 * my_pi),1.0/roottwo));
- assert(roughly(cos(-0.5 * my_pi),0.0));
- assert(roughly(cos(-0.75 * my_pi),-1.0/roottwo));
- assert(roughly(cos(-1.0 * my_pi),-1.0));
-
- assert(roughly(tan(0.0),0.0)); // --- test tangent -pi to +pi
- assert(roughly(tan(0.25 * my_pi),1.0));
- assert(roughly(tan(0.75 * my_pi),-1.0));
- assert(roughly(tan(1.0 * my_pi),0.0));
-
- assert(roughly(tan(-0.25 * my_pi),-1.0));
- assert(roughly(tan(-0.75 * my_pi),1.0));
- assert(roughly(tan(-1.0 * my_pi),0.0));
- printf("passed sin/cos/tan...\n");
-
- // --- log, log10
- assert(roughly(log(my_e),1.0)); // --- test log and log10
- assert(roughly(log(1.0),0.0));
- assert(roughly(log(my_e + my_e), 1.0 + log(2.0)));
- assert(roughly(log(my_e * my_e * 2.0), 2.0 + log(2.0)));
-
- assert(roughly(log10(10.0),1.0));
- assert(roughly(log10(1.0),0.0));
- assert(roughly(log10(10.0 + 10.0), 1.0 + log10(2.0)));
- assert(roughly(log10(10.0 * 10.0 * 2.0), 2.0 + log10(2.0)));
- printf("passed log/log10... ");
-
- // --- exp, pow
- assert(roughly(exp(0), 1.0)); // --- test exp and pow
- assert(roughly(exp(1.0), my_e));
- assert(roughly(exp(2.0), my_e * my_e));
- assert(roughly(exp(-2.0),1.0/(my_e * my_e)));
- assert(roughly(exp(-3.0),1.0/(my_e * my_e * my_e)));
- assert(roughly(exp(log(my_pi)),my_pi));
-
- assert(roughly(pow(2.0,2.0), 4.0));
- assert(roughly(pow(-3.0,3.0), -27.0));
- assert(roughly(pow(2.0,0.5), roottwo));
- assert(roughly(pow(4.0,0.25), roottwo));
- assert(roughly(pow(2.0,-4.0), 0.0625));
- assert(roughly(pow(1.5,2.0), 2.25));
- assert(roughly(pow(-1.5,3.0), -3.375));
- printf("passed exp/pow... ");
-
- // --- atan2
- assert(roughly(atan2(0.0,0.0), 0.0)); // --- test atan2 ... we define atan2(0,0)=0
- assert(roughly(atan2(0.0,1.0), 0.0)); // --- y=0, x=1
- assert(roughly(atan2(0.0,-1.0), my_pi)); // --- y=0, x=-1
-
- assert(roughly(atan2(1.0,0.0), my_pi/2.0));
- assert(roughly(atan2(1.0,-1.0), my_pi*3.0/4.0));
- assert(roughly(atan2(1.0,1.0), my_pi/4.0));
-
- assert(roughly(atan2(-1.0,0.0), -my_pi/2.0));
- assert(roughly(atan2(-1.0,-1.0), -my_pi*3.0/4.0));
- assert(roughly(atan2(-1.0,1.0), -my_pi/4.0));
- printf("passed atan2...\n");
-
-
- // --- test the MATHFUNC functions
- // -------------------------------
-
- // --- fabs
- assert(roughly(fabs(0.0), 0.0)); // --- test fabs
- assert(roughly(fabs(my_pi), my_pi));
- assert(roughly(fabs(-my_pi), my_pi));
- printf("passed fabs... ");
-
- // --- sinh/cosh/tanh
- assert(roughly(sinh(0.0), 0.0)); // --- test sinh
- assert(roughly(sinh(1.0), (my_e - 1.0/my_e)/2.0)); // --- exp(1)=my_e
- assert(roughly(sinh(-1.0), ((1.0/my_e) - my_e)/2.0)); // --- exp(-1)=1/my_e
-
- assert(roughly(cosh(0.0), 1.0)); // --- test cosh
- assert(roughly(cosh(1.0), (my_e + 1.0/my_e)/2.0)); // --- exp(1)=my_e
- assert(roughly(cosh(-1.0), (my_e + 1.0/my_e)/2.0));
-
- assert(roughly(tanh(0.0), 0.0)); // --- test tanh
- assert(roughly(tanh(1.0), (my_e*my_e - 1.0) / (my_e*my_e + 1.0)));
- assert(roughly(tanh(-1.0), -(my_e*my_e - 1.0) / (my_e*my_e + 1.0)));
- printf("passed sinh/cosh/tanh... ");
-
- // --- asinh/acosh/atanh
- assert(roughly(asinh(0.0), 0.0)); // --- test asinh
- assert(roughly(asinh(1.0), log(1.0 + roottwo))); // --- because sinh(log(1+roottwo))==1
- assert(roughly(asinh(-1.0), -log(roottwo + 1.0))); // --- because sinh(-log(roottwo+1))==-1
-
- assert(roughly(acosh(1.0), 0.0)); // --- test acosh
- assert(roughly(acosh(roottwo), log(roottwo+1))); // --- because cosh(log(1+roottwo))=roottwo
-
- assert(roughly(atanh(0.0), 0.0)); // --- test atanh
- assert(roughly(atanh(0.5), 0.5 * log(3.0))); // --- because tanh(0.5 * log(3))=0.5
- printf("passed asinh/acosh/atanh...\n");
-
- // --- asin/acos/atan
- assert(roughly(asin(0.0), 0.0)); // --- test asin
- assert(roughly(asin(1.0), my_pi / 2.0));
- assert(roughly(asin(-1.0), -my_pi / 2.0));
- assert(roughly(asin(1.0/roottwo), my_pi / 4.0));
- assert(roughly(asin(-1.0/roottwo), -my_pi / 4.0));
-
- assert(roughly(acos(1.0), 0.0)); // --- test acos
- assert(roughly(acos(0.0), my_pi / 2.0));
- assert(roughly(acos(-1.0), my_pi));
- assert(roughly(acos(1.0/roottwo), my_pi / 4.0));
- assert(roughly(acos(-1.0/roottwo), 3.0 * my_pi / 4.0));
-
- assert(roughly(atan(0.0), 0.0)); // --- test atan
- assert(roughly(atan(1.0), my_pi / 4.0));
- assert(roughly(atan(-1.0), -my_pi / 4.0));
- printf("passed asin/acos/atan...\n");
-
- printf("==> finished xmath <==\n");
- return;
- }
-